package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SPH_Demo01
 * 2022/11/3 09 36
 */
@RestController
@RequestMapping ("/admin/product/baseTrademark")
public class TrademarkController {
    @Autowired
    BaseTrademarkService baseTrademarkService;

    /**
     * Trademark的分页显示
     * @param pn：第几页
     * @param ps：每页多少条数据
     * @return
     */
    @GetMapping ("/{page}/{limit}")
    public Result getPageTrademark(@PathVariable("page") Long pn,@PathVariable("limit") Long ps){
        IPage<BaseTrademark> page = new Page<> (pn,ps);
        baseTrademarkService.page (page);
        return Result.ok (page);
    }

    /**
     * 新增Trademark信息
     * @param baseTrademark
     * @return
     */
    @PostMapping("/save")
    public Result addTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.save (baseTrademark);
        return Result.ok ();
    }

    /**
     * 修改Trademark信息
     * @param baseTrademark
     * @return
     */
    @PutMapping("/update")
    public Result updateTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.saveOrUpdate (baseTrademark);
        return Result.ok ();
    }

    /**
     * 删除Trademark信息：通过Id
     * @param id：品牌信息的Id
     */
    @DeleteMapping("/remove/{id}")
    public Result deleteTrademarkById(@PathVariable Long id){
        QueryWrapper<BaseTrademark> queryWrapper = new QueryWrapper<> ();
        queryWrapper.eq ("id",id);
        baseTrademarkService.remove (queryWrapper);
        return Result.ok ();
    }

    /**
     * 根据Id获取品牌
     * @param id: 品牌id
     */
    @GetMapping("/get/{id}")
    public Result getTrademarkById(@PathVariable Long id){
        BaseTrademark trademark = baseTrademarkService.getById (id);
        return Result.ok (trademark);
    }

    //http://127.0.0.1/admin/product/baseTrademark/getTrademarkList

    /**
     * 查询所有品牌
     * @return List ->所有品牌信息
     */
    @GetMapping("/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> list = baseTrademarkService.list ();
        return Result.ok (list);
    }

}
